package threadx;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class Test8 {

    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    private ReadLock readLock = rwLock.readLock();
    private WriteLock writeLock = rwLock.writeLock();

    public void read() {
        readLock.lock();
        try {
            System.out.println("当前线程:" + Thread.currentThread().getName() + "拿到读锁，进入...");
            Thread.sleep(8000);
            System.out.println("当前线程:" + Thread.currentThread().getName() + "释放读锁，退出...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
    }

    public void write() {
        writeLock.lock();
        try {
            System.out.println("当前线程:" + Thread.currentThread().getName() + "拿到写锁，进入...");
            Thread.sleep(8000);
            System.out.println("当前线程:" + Thread.currentThread().getName() + "释放写锁，退出...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
    }

    public void nolock() {
        try {
            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，进入...");
            Thread.sleep(8000);
            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，退出...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        final Test8 t = new Test8();

//        try {
//            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，进入...");
//            Thread.sleep(8000);
//            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，退出...");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        Thread t0 = new Thread(new Runnable() {
            @Override
            public void run() {
                t.nolock();
            }
        }, "t0");

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                t.read();
            }
        }, "t1");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                t.read();
            }
        }, "t2");
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                t.write();
            }
        }, "t3");
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                t.write();
            }
        }, "t4");
//        t0.start();
//        t1.start();
//        t2.start();

//        t0.start();
//		t1.start(); // R
//		t3.start(); // W

//        t0.start();
        t3.start();
        t4.start();

        try {
            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，进入...");
            Thread.sleep(8000);
            System.out.println("当前线程:" + Thread.currentThread().getName() + "无锁，退出...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

